global name "main_figures"
cap log close
log using "${pfile}/${name}.smcl", replace smcl


* set baseline bandwidth
global band = 0.075
global bandui = 6
global bandrdbirth = 90


// ******************************  // FIGURE 1 B2 B3 B5 // ****************************** //
use A_cash, clear

* DENSITY

global bsize = 0.005 // bin size
global band_max = 0.4 // graph range

g y = x if x == 0 // y will represents the mass point at x=0 in the graph
replace x = . if x==0 // dropping workers at the threshold

keep x y year 

// For Optimal AIC Regressions
global p1 "kxb xbin"
forvalues k = 2/5{
local k_1 = `k'-1
global p`k' "${p`k_1'} kxb`k' xb`k'"
di "${p`k'}"
}

// McCrary test
cap drop Xj Yj r0 fhat se_fhat
DCdensity x if abs(x) <= 0.8, breakpoint(0) h(.15) generate(Xj Yj r0 fhat se_fhat) nograph  /*graphname(DCdensity_example.eps)*/

local mc_b = round(`r(theta)'*1000)/1000
local mc_se = round(`r(se)'*1000)/1000

* First Derivative Density Discontinuity Test
egen xbin = cut(x), at(-${band_max}(${bsize})${band_max})
replace xbin = xbin + ${bsize}/2

collapse (count) x y, by(xbin)
g k = xbin > 0
g kxb = xbin * k
g xb2 = xbin^2
g xb3 = xbin^3
g xb4 = xbin^4
g xb5 = xbin^5

g kxb2 = xb2 * k
g kxb3 = xb3 * k
g kxb4 = xb4 * k
g kxb5 = xb5 * k

*** Optimal AIC Order ***
reg	x ${p2}
scalar aic_p = -2*e(ll)+2*(e(N)-e(df_r))
scalar aic_ord = 2

reg	x ${p3}
scalar aic_3 = -2*e(ll)+2*(e(N)-e(df_r))

reg	x ${p4}
scalar aic_4 = -2*e(ll)+2*(e(N)-e(df_r))

reg	x ${p5}
scalar aic_5 = -2*e(ll)+2*(e(N)-e(df_r))

if (aic_p > aic_3) {
scalar aic_p = aic_3
scalar aic_ord = 3
}

else if aic_p > aic_4 {
scalar aic_p = aic_4
scalar aic_ord = 4
}

else if aic_p > aic_5 {
scalar aic_p = aic_5
scalar aic_ord = 5
}

di "opt order is"
di aic_ord

reg x ${p`=scalar(aic_ord)'}

cap drop x_pred
predict x_pred, xb
label var x_pred "Polynomial Fit"

replace x = x/1000
replace y = y/1000
replace x_pred = x_pred/1000

su x if xbin != .
local horz = .1
local max = int(r(max))
local max_2 = int(r(max)*3/2)
local max_int = int(`max_2'/3)
local vert =  1.25 * r(max)

di `vert'
di `horz'

replace x = . if xbin == .
replace y = . if xbin != .
replace xbin = 0 if xbin == .

* Graph
twoway (scatter x xbin if xbin < ${band_max} & xbin > - ${band_max},  title(Density, size(med)) ylabel(0(`max_int')`max_2') msize(tiny) mcolor(black) ylabel(, glcolor(gs15)) xline(0) xtitle(Previous year earnings around 2 min wages,  size(small)) ///
ytitle( Obs x 1000, size(small)) xlabel( ,labsize(small))  ylabel(,labsize(small)) graphregion(fcolor(white) color(white)) legend(off) name("Dens_", replace) ///
ttext( `vert' `horz' "Degree Fit: `=scalar(aic_ord)' - McCrary Discontinuity Estimate = `mc_b' (`mc_se')",  just(left) size(vsmall)) ) ///
(scatter y xbin if xbin < ${band_max} & xbin > - ${band_max},  msize(vsmall) msymbol(circle) mcolor(red) ) ///
(connected x_pred xbin if xbin <0, msize(vtiny)  lwidth(med) ) (connected x_pred xbin if xbin >=0, msize(vtiny)  lwidth(med) ) ///
, aspectratio(1.01) graphregion(margin(0 0 0 0))  plotregion(margin(0 0 0 0)) // 

graph save "${pgraph}/Figcash_density.gph", replace
graph export "${pgraph}/Figcash_density.png", replace as(png) width(1000) height(1000)


* OUTCOMES
use A_cash, clear

global var "cash sur3 sur36 dur6 n_remmedr totinc totwage totmon tempempr remmedia p_tempempr school white gend hor age_dism" //
global bin_size = 0.025
global range = 0.4
global kernel "tri"

* Selecting variables
keep ${var} x year cpf

// Labels
label var cash "Eligible for the grant"
label var n_remmedr "Next Job - Monthly Earnings"
label var totinc "Total Earnings - 3 years"
label var totwage "Av. Earnings - 3 years"
label var totmon "Months Worked - 3 years"
label var dur6 "Duration - 6 Months" 
label var sur3  "Survival - 3 months"
label var sur36 "Survival - 3 years"
label var tempempr "Tenure at Lay-off (weeks)"
label var remmedia "Monthly Earnings (min. wages)"
label var p_tempempr "Tenure at Previous Job (weeks)"
label var white "Prob(Worker is white)"
label var age_dism "Age at Dismissal"


******************************************************
g x0 = x if x==0
replace x = . if x == 0
gegen xb = cut(x), at(-${range}(${bin_size})${range})
replace xb = xb + ${bin_size}/2
gegen tag = tag(xb)

foreach var of global var {

	local new = `new' + 1

	global b = 0.075
	
	cap drop av_`var' av0_`var'
	gegen av_`var' = mean(`var'), by(xb)
	gegen av0_`var' = mean(`var'), by(x0)
	
	gsort x0
	by x0: replace tag = 1 if x0==0 & _n==1

	graph twoway (scatter av_`var' xb if tag == 1 & abs(x) <= ${range}, msize(small) msymbol(triangle) ylabel(, glcolor(gs15)) xline(0, lwidth(medthick)) ) ///
	(scatter av0_`var' x0 if tag == 1, msize(medsmall) msymbol(triangle) mcolor(red) ylabel(, glcolor(gs15)) xline(0, lwidth(medthick)) ) ///
	(lpoly `var' x if x<= 0 & abs(x) <= ${range},  lcolor(black) lwidth(medthick) degree(1) kernel(${kernel}) bwidth(${b}) ) ///
	(lpoly `var' x if x>0 & abs(x) <= ${range}, lcolor(black) lwidth(medthick) degree(1) kernel(${kernel}) bwidth(${b}) ), ///
	ytitle("") xtitle(Previous year earnings around 2 min wages) title(`: variable label `var'') legend(off) name(`var',replace) graphregion(fcolor(white) color(white))
		
	graph export "${pgraph}/Figcash_`var'.png", replace as(png)
	graph save "${pgraph}/Figcash_`var'.gph", replace
	graph close _all
}


*** Figure 1
foreach x in density sur3 sur36 dur6 {
	graph use "${pgraph}/Figcash_`x'.gph", name(`x', replace)
	}

graph combine density sur3, title() name(out1, replace)  rows(1) xsize(20) ysize(10) graphregion(fcolor(white) color(white))
graph combine sur36 dur6, title() name(out2, replace)  rows(1) xsize(20) ysize(10) graphregion(fcolor(white) color(white))

graph combine out1 out2, title() name(main, replace)  rows(2) xsize(10) ysize(10) graphregion(fcolor(white) color(white))  imargin(0 0 0 0)  // graphregion(margin(l=0 r=0))
graph export "${pgraph}/Figure1.png", replace as(png) name(main)  width(2000) height(2000)
graph close

*** Figure B2
foreach x in tempempr remmedia p_tempempr school white gend hor age_dism {
	graph use "${pgraph}/Figcash_`x'.gph", name(`x', replace)
	}


graph combine tempempr remmedia p_tempempr school, title() name(cov_A, replace)  graphregion(fcolor(white) color(white))
graph combine white gend hor age_dism, title() name(cov_B, replace)  graphregion(fcolor(white) color(white))
	
graph export "${pgraph}/FigureB2_1.png", replace as(png) name(cov_A)  width(1400) height(1000)
graph export "${pgraph}/FigureB2_2.png", replace as(png) name(cov_B)  width(1400) height(1000)
graph close

*** Figure B3 ***
graph use "${pgraph}/Figcash_cash.gph", name(cash, replace)

graph export "${pgraph}/FigureB3.png", replace as(png)
graph close

*** Figure B5
foreach x in n_remmedr totinc totmon totwage {
	graph use "${pgraph}/Figcash_`x'.gph", name(`x', replace)
	}

graph combine n_remmedr totinc, title() name(out1, replace)  rows(1) xsize(20) ysize(10) graphregion(fcolor(white) color(white))
graph combine totmon totwage, title() name(out2, replace)  rows(1) xsize(20) ysize(10) graphregion(fcolor(white) color(white))

graph combine out1 out2, title() name(main, replace)  rows(2) xsize(10) ysize(10) graphregion(fcolor(white) color(white))  imargin(0 0 0 0)  // graphregion(margin(l=0 r=0))
graph export "${pgraph}/FigureB5.png", replace as(png) name(main)  width(2000) height(2000)

graph close

*** Deleting single graphs
foreach x in density sur3 sur36 dur6 cash n_remmedr totinc totmon totwage tempempr remmedia p_tempempr school white gend hor age_dism {
	cap erase "${pgraph}/Figcash_`x'.gph"
	cap erase "${pgraph}/Figcash_`x'.png"
	}
	


// ******************************  // FIGURE 2 // ****************************** //
use A_cash, clear

// matrix for storing results
set matsize 1000
mat res = J(1000,25,.)
mat coln res = av_dur dep disc se t sig disc_b se_b t_b sig_b bsel band band_b b_f pol obs control mon_i mon_f ten_min ten_max cause drop_x0 drop_mer note

drop if x==0

global var "dur1 dur2 dur3 dur4 dur5 dur6 dur7 dur8 dur9 dur10 dur11 dur12 dur13 dur14 dur15 dur16 dur17 dur18 dur19 dur20 dur21 dur22 dur23 dur24 dur25 dur26 dur27 dur28 dur29 dur30 dur31 dur32 dur33 dur34 dur35 dur36 sur1 sur2 sur3 sur4 sur5 sur6 sur7 sur8 sur9 sur10 sur11 sur12 sur13 sur14 sur15 sur16 sur17 sur18 sur19 sur20 sur21 sur22 sur23 sur24 sur25 sur26 sur27 sur28 sur29 sur30 sur31 sur32 sur33 sur34 sur35 sur36" //

// create vars
forval x = 1/36 { // everything monthly
cap g sur`x' = und > 30/7 * (`x')
cap g dur`x' = und
replace dur`x' = 30/7 * `x' if und >= 30/7 * `x'
}

// selecting variables
keep ${var} k kx x year cpf // dt_dism tempempr school ind race gend hor year uf remmedr p_causadesli p_tempempr 

keep if abs(x)<=${band}
******		RDD ON EACH VAR		******
local new = 0     // matrix line to write results
local var_n = 0   // var number
foreach var of global var {   
	local var_n = `var_n' + 1  

	qui su `var' if x > 0 & x < 0.05
	local av_dur_`var' = r(mean)  // Average level to the right of the threshold

	// Regressions

	forvalues p = 1/1 {

			local band = ${band}
	
			local new = `new' + 1
			reg `var' k kx x if abs(x)<= `band' [w=(`band'-abs(x))/`band'], vce(cluster cpf)
									
			mat res[`new',colnumb(res,"dep")] = `var_n'
			mat res[`new',colnumb(res,"disc")] = _b[k]
			mat res[`new',colnumb(res,"se")] =  _se[k]
			mat res[`new',colnumb(res,"t")] = abs(_b[k]/_se[k])  
			mat res[`new',colnumb(res,"bsel")] = 1
			mat res[`new',colnumb(res,"pol")] = `p'   
			mat res[`new',colnumb(res,"band")]  = `band'
			mat res[`new',colnumb(res,"obs")] =	e(N)
			mat res[`new',colnumb(res,"control")] = 0			
			mat res[`new',colnumb(res,"av_dur")] = `av_dur_`var''
			*/
			}
	}
			
clear

svmat res, names(col)

tostring dep sig sig_b bsel note, replace

local r = 0
foreach var of global var {
local r = `r' + 1
replace dep = "`var'" if dep == "`r'"
}

drop if dep == "."

rename b_f md

g time  = substr(dep,4,2)
destring time, replace

replace dep = substr(dep,1,3)

g upper = disc + 1.965 * abs(se)
g lower = disc - 1.965 * abs(se)

global haz "-0.02 -0.01 0 0.01 0.02 0.03"
global sur "-0.02 -0.01 0 0.01 0.02 0.03"
global dur "-1 0 1 2 3"

global yhaz "Hazard Rate"
global ysur "Survival Rate"
global ydur "Censored Duration"

foreach dep in sur dur { // haz 
twoway  (scatter disc time if dep == "`dep'", mcolor(gs2) ) ///
(rcap upper lower time if dep == "`dep'", lcolor(gs2)  lp()), ///
yline(0, lcolor(gs4)) ylabel( ${`dep'} , grid format(%12.2f)) xlabel(3(3)36, grid glcolor(gs16)) legend(off) ///
name(`dep',replace)  graphregion(fcolor(white) color(white)) ytitle(${y`dep'}, size(medsmall)) xtitle("Months since dismissal",size(small)) ///
title("${md`md'}",size(medsmall))
}

graph combine sur dur , name(surv_eff,replace) graphregion(fcolor(white) color(white)) col(1) // haz 
graph export "${pgraph}/Figure2.png", replace as(png) name(surv_eff)  

graph close


// ******************************  // FIGURE 3 // ****************************** //

global varmatch "year uf dtbirth gend race large" // metro  race school
global pop = 1000000

use "B2_mergeadmin.dta", clear

merge m:1 cpf using B5_racemode, keep(1 3) nogen keepus(racedismbmodemax)
replace race = racedismbmodemax if race != racedismbmodemax & racedismbmodemax !=.
drop racedismbmodemax

g large = pop>1000000

replace school = school / 100

bys $varmatch: g co = _N
label var co "Cluster Size"
keep if co <=10 
				
merge m:1 $varmatch using "B4_pnad_merge_collapse.dta", nogen keep(1 3) keepus(emp_form emp_informal emp_self id_survey p_ui p_ten w w_form w_inf) // race school

g matched = emp_form!=.
g age = (mdy(12,31,year)-dtbirth)/365
g emp_inf2 = emp_info + emp_self

// Graph //
keep if matched == 1

global bin_size = 0.25
global band_l = 16
global band_r = 30
global ci = 0          // ci = 1 for confidence intervals

egen xb = cut(tempempr), at(-${band_l}(${bin_size})${band_r})
replace xb = xb + ${bin_size}/2
egen tag = tag(xb)

global b_reg = 6  // bandwidth for RD regression line
global b_graph = 6  // Bandwidth range for fitted values

label var emp9 "Formal Job September - Admin Data"
label var emp_form "Formal Job September - Survey Data"
label var emp_inform "Informal Job September - Survey Data"
label var emp_self "Self-Employed September - Survey Data"

cap drop k x kx
g x = tempempr - 24
g k = x >=0
g kx = k*x

foreach var in emp9 emp_form emp_inform emp_self p_ui p_ten {
	cap drop av_`var'
	bysort xb: egen av_`var' = mean(`var')
	
	qui reg `var' k kx x if tempempr >= 22 - ${b_reg} & tempempr <= 24 + ${b_reg} & (x<=-2|x>0) 
	predict est_`var', xb

	su av_`var' if tag == 1 & tempempr >= ${band_l} & tempempr <= ${band_r}
	local r_min = round(r(min)*0.9,0.001)
	local r_max = round(r(max)*1.1,0.001)
	di `r_min'
	di `r_max'
	
		if ${ci} == 0 {	
		graph twoway (scatter av_`var' xb if tag == 1 & tempempr >= ${band_l} & tempempr <= ${band_r}, msize(small) msymbol(triangle) xline(22 24, lwidth(medthick)) ) ///
		(line est_`var' tempempr if tag == 1 & tempempr >= 22-${b_graph} & tempempr < 24,  lcolor(black) lwidth(medthick) )  ///
		(line est_`var' tempempr if tag == 1 & tempempr >= 24 & tempempr <= 24+${b_graph},  lcolor(black) lwidth(medthick) ) , ///
		ytitle("") xtitle(Tenure at Lay-off around 22/24 months) title(`: variable label `var'') legend(off) name(`var',replace)  graphregion(fcolor(white) color(white)) ///
		yscale(r(`r_min' `r_max')) ylabel(`r_min' `r_max') xscale(r(16 30)) xlabel(16 18 20 22 24 26 28 30)
		}

		if ${ci} == 1 {	
		graph twoway (scatter av_`var' xb if tag == 1 & abs(x) <= ${band}, msize(small) xline(0, lwidth(medthick)) ) ///
		(lpolyci `var' x if x<0 & abs(x) <= ${band},  lcolor(black) lwidth(medthick) alwidth(thin) degree(1) kernel(rec) ciplot(rline) bwidth(${ik_`var'}) pwidth(${ik_`var'})   ) ///
		(lpolyci `var' x if x>=0 & abs(x) <= ${band},  lcolor(black) lwidth(medthick)   alwidth(thin) degree(1) kernel(rec)  ciplot(rline)  bwidth(${ik_`var'}) pwidth(${ik_`var'}) ), ///
		ytitle("") xtitle(Tenure at Lay-off around 22/24 months) title(`: variable label `var'') legend(off) name(`var',replace)  graphregion(fcolor(white) color(white))
		}
	
	cap drop est_`var'
	}
	
	graph combine emp9 emp_inform emp_form emp_self , title() name(rr9_tenout, replace)  graphregion(fcolor(white) color(white)) rows(2) cols(2) // xsize(20) ysize(10)
	graph export "${pgraph}/Figure3.png", replace as(png) name(rr9_tenout)   width(2000) // height(2400)
	
	graph close
	
*/



// ******************************  // FIGURE B1 // ****************************** //

// *** // Graphs // *** //
// 2013
use FS2013, clear

keep if abs(x)<=0.6
twoway ///
(scatter above_p10 x if abs(x)<=0.6 & x!=0, xline(0) msize(vsmall)) ///
(scatter above_p10 x if x == 0.0 , xline(0) msize(small) mcolor(red)), ///
ytitle("") title(Share earning above 2.1 m.w. at least one month 2014) xlabel(-.5 .5 ,labsize(med))  ///
ylabel(,labsize(vlarge)) graphregion(fcolor(white) color(white)) legend(off) name(share10_2013, replace)


//2014
use FS2014, replace

keep if abs(x)<=0.6
twoway ///
(scatter abono x if abs(x)<=0.6 & x!=0, xline(0) msize(vsmall)) ///
(scatter abono x if x == 0.0, xline(0) msize(small) mcolor(red)), ///
ytitle("") title(Prob. Worker is Eligible and Recalled for Take-up 2015) xlabel(-.5 .5 ,labsize(med))  ///
ylabel(,labsize(vlarge)) graphregion(fcolor(white) color(white)) legend(off) name(fs2014, replace)

graph combine fs2014 share10_2013, title() name(fs, replace)  graphregion(fcolor(white) color(white)) rows(2) xsize(7) ysize(10) 
graph export "${pgraph}/FigureB1.png", replace as(png) width(1400) height(2000) name(fs)

graph close


// ******************************  // FIGURE B4 // ****************************** //
use A_cash, clear

drop if x==0

global var "sur3 sur36 dur6 dur36 n_remmedr totinc totmon totwage"
// matrix for storing results
set matsize 10000
mat res = J(10000,26,.)
mat coln res = cutoff av_dur dep disc se t sig disc_b se_b t_b sig_b bsel band band_b b_f pol obs control mon_i mon_f ten_min ten_max cause drop_x0 drop_mer note

rename x xorig
keep ${var} xorig year cpf // dt_dism tempempr school ind race gend hor year uf remmedr p_causadesli p_tempempr 

// RD variables
local new = 0     // matrix line to write results
qui forval c = -.4(.01).4 {
	cap drop k x kx
	g x = xorig + `c'
	g k = (x<=0 & x!=.)
	g kx = k * x

	******		RDD ON EACH VAR		******
	local var_n = 0   // var number
	foreach var of global var {   
		local var_n = `var_n' + 1  

		qui su `var' if x > 0 & x < 0.05
		local av_dur_`var' = r(mean)  // Average level to the right of the threshold

		// Regressions
		forvalues p = 1/1 {

			local band = 0.075
			
			local b = `band' * 1.5
			
			local new = `new' + 1
			
			reg `var' k kx x if abs(x)<= `band' [w=(`band'-abs(x))/`band'], vce(cluster cpf)
									
			mat res[`new',colnumb(res,"dep")] = `var_n'
			mat res[`new',colnumb(res,"disc")] = _b[k]
			mat res[`new',colnumb(res,"se")] =  _se[k]
			mat res[`new',colnumb(res,"t")] = abs(_b[k]/_se[k])  
			mat res[`new',colnumb(res,"bsel")] = 1
			mat res[`new',colnumb(res,"pol")] = `p'   
			mat res[`new',colnumb(res,"band")]  = `band'
			mat res[`new',colnumb(res,"obs")] =	e(N)
			mat res[`new',colnumb(res,"control")] = 0			
			mat res[`new',colnumb(res,"av_dur")] = `av_dur_`var''
			mat res[`new',colnumb(res,"cutoff")] = `c'
		}
	}
}			
			
clear
svmat res, names(col)

tostring dep sig sig_b bsel note, replace

local r = 0
foreach var of global var {
local r = `r' + 1
replace dep = "`var'" if dep == "`r'"
}

// Labels
global lab_n_remmedr "Monthly Earnings"
global lab_totinc "Total Earnings"
global lab_totwage "Av. Earnings"
global lab_totmon "Months Worked"
global lab_dur6 "Duration - 6 months" 
global lab_dur36 "Duration - year 3" 
global lab_sur3  "Survival - 3 months"
global lab_sur36 "Survival - 3 years"

save A_cash_temp, replace
// Graphs Permutation //
foreach var in $var {
*preserve
keep if dep=="`var'"

* actual estimate
su disc if dep=="`var'" & abs(cutoff)<=.005
global beta = r(mean)

*placebo distribution
_pctile disc if dep=="`var'" & abs(cutoff)>=.005, p(2.5 5 95 97.5)
global lo = r(r1)
global hi = r(r4)
global lob = r(r2)
global hib = r(r3)
global wid = abs(${lo} - ${hi})/25

hist disc if dep=="`var'" & abs(cutoff)>=.005, xtitle("Discontinuity estimates") ///
width(${wid}) xline(${beta}, lc(black)) xline(${lo} ${hi}, lcolor(red) lp(dash)) xline(${lob} ${hib}, lcolor(maroon) lp(dash))  bcolor(gs7) fcolor(gs14) ///
ytitle("") title(${lab_`var'}) legend(off) name(`var',replace) graphregion(fcolor(white) color(white)) yl(#3) xlabels(0 ${beta}, format(%9.2f))


use A_cash_temp, clear

}
erase A_cash_temp.dta

graph combine ${var}, title() name(cash_perm, replace) rows(4) graphregion(fcolor(white) color(white)) xsize(8) ysize(10) imargin(0.75 0.75 0 0)  // graphregion(margin(l=0 r=0))
graph export "${pgraph}/FigureB4.png", replace as(png) width(1400)

graph close



// ******************************  // FIGURE B6 // ****************************** //
use A_cash, clear

global outcome "sur3 dur6 dur36 n_jdur n_remmedr totinc totmon totwage"

* only eligible workers
keep if x<0
drop k kx x

g t = dtb - mdy(7,1,year(dtb))
g k = t>=0
g kt = k * t

cap drop tbin
egen tbin = cut(t), at(-180(10)180)
replace tbin = tbin + 10/2
drop if tbin <= -180 | tbin >= 180
gegen tag = tag(tbin)

label var n_remmedr "Next Job - Monthly Earnings"
label var n_jdur "Next Job - Duration"
label var totinc "Total Earnings"
label var totwage "Av. Earnings"
label var totmon "Months Worked"
label var dur6 "Duration - 6 months" 
label var dur36 "Duration - 36 months" 
label var sur3  "Survival - 3 months"

graph close
foreach var in $outcome {
global n = $n + 1

cap gegen av_`var' = mean(`var'), by(tbin)

graph twoway (scatter av_`var' t if tag==1, msize(small) msymbol(triangle) ylabel(, glcolor(gs15)) xline(0, lwidth(medthick)) ) ///
(lpoly `var' t if t< 0, lcolor(black) lwidth(medthick) degree(1) kernel(rec) bwidth(${bandrdbirth}) ) ///
(lpoly `var' t if t>=0, lcolor(black) lwidth(medthick) degree(1) kernel(rec) bwidth(${bandrdbirth}) ), ///
ytitle("") xtitle("Date of birth relative to July 1st") ylabels(#2) xlabels(-180 -90 0 90 180) title(`: variable label `var'') legend(off) name(`var',replace) graphregion(fcolor(white) color(white))

}

graph combine sur3 dur6 dur36 n_jdur n_remmedr totinc totwage totmon, title() name(out, replace)  rows(4) xsize(7) ysize(10) graphregion(fcolor(white) color(white)) // sur6 
graph export "${pgraph}/FigureB6.png", replace as(png) name(out)  width(1400) height(2000)
graph close


// ******************************  // FIGURE B7 // ****************************** //
use A_cash, clear

keep if year==2014
global outcome "sur3 dur6 dur36 n_jdur n_remmedr totinc totmon totwage"

* only eligible workers
keep if x<0
drop k kx x

g t = dtb - mdy(7,1,year(dtb))
g k = t>=0
g kt = k * t

cap drop tbin
egen tbin = cut(t), at(-180(10)180)
replace tbin = tbin + 10/2
drop if tbin <= -180 | tbin >= 180
gegen tag = tag(tbin)

label var n_remmedr "Next Job - Monthly Earnings"
label var n_jdur "Next Job - Duration"
label var totinc "Total Earnings"
label var totwage "Av. Earnings"
label var totmon "Months Worked"
label var dur6 "Duration - 6 months" 
label var dur36 "Duration - 36 months" 
label var sur3  "Survival - 3 months"

graph close
foreach var in $outcome {
global n = $n + 1

cap gegen av_`var' = mean(`var'), by(tbin)

graph twoway (scatter av_`var' t if tag==1, msize(small) msymbol(triangle) ylabel(, glcolor(gs15)) xline(0, lwidth(medthick)) ) ///
(lpoly `var' t if t< 0, lcolor(black) lwidth(medthick) degree(1) kernel(rec) bwidth(${bandrdbirth}) ) ///
(lpoly `var' t if t>=0, lcolor(black) lwidth(medthick) degree(1) kernel(rec) bwidth(${bandrdbirth}) ), ///
ytitle("") xtitle("Date of birth relative to July 1st") ylabels(#2) xlabels(-180 -90 0 90 180) title(`: variable label `var'') legend(off) name(`var',replace) graphregion(fcolor(white) color(white))

}

graph combine sur3 dur6 dur36 n_jdur n_remmedr totinc totwage totmon, title() name(out, replace)  rows(4) xsize(7) ysize(10) graphregion(fcolor(white) color(white)) // sur6 
graph export "${pgraph}/FigureB7.png", replace as(png) name(out)  width(1400) height(2000)
graph close




// ******************************  // FIGURE B8 - PART 1 // ****************************** //
use A_cash, clear

set matsize 10000
mat res = J(10000,26,.)
mat coln res = cutoff av_dur dep disc se t sig disc_b se_b t_b sig_b bsel band band_b b_f pol obs control mon_i mon_f ten_min ten_max cause drop_x0 drop_mer note

global var "dur1 dur2 dur3 dur4 dur5 dur6 dur7 dur8 dur9 dur10 dur11 dur12 dur13 dur14 dur15 dur16 dur17 dur18 dur19 dur20 dur21 dur22 dur23 dur24 dur25 dur26 dur27 dur28 dur29 dur30 dur31 dur32 dur33 dur34 dur35 dur36 sur1 sur2 sur3 sur4 sur5 sur6 sur7 sur8 sur9 sur10 sur11 sur12 sur13 sur14 sur15 sur16 sur17 sur18 sur19 sur20 sur21 sur22 sur23 sur24 sur25 sur26 sur27 sur28 sur29 sur30 sur31 sur32 sur33 sur34 sur35 sur36" //

forval x = 1/36 { // everything monthly
cap g sur`x' = und > 30/7 * (`x')
cap g dur`x' = und
replace dur`x' = 30/7 * `x' if und >= 30/7 * `x'
}

* only eligible workers
keep if x<0
drop k kx x

g t = dtb - mdy(7,1,year(dtb))
g k = t>=0
g kt = k * t

******		RDD ON EACH VAR		******
local new = 0     // matrix line to write results
local var_n = 0   // var number
foreach var of global var {   
	local var_n = `var_n' + 1  

	qui su `var' if t > 0 & t <= ${bandrdbirth}
	local av_dur_`var' = r(mean)  // Average level to the right of the threshold

	// Regressions
	local new = `new' + 1
	qui reg `var' k kt t if abs(t)<= ${bandrdbirth} , vce(cluster cpf)
							
	mat res[`new',colnumb(res,"dep")] = `var_n'
	mat res[`new',colnumb(res,"disc")] = _b[k]
	mat res[`new',colnumb(res,"se")] =  _se[k]
	mat res[`new',colnumb(res,"t")] = abs(_b[k]/_se[k])  
	mat res[`new',colnumb(res,"pol")] = 1   
	mat res[`new',colnumb(res,"band")]  = ${bandrdbirth}
	mat res[`new',colnumb(res,"obs")] =	e(N)
	mat res[`new',colnumb(res,"control")] = 0			
	mat res[`new',colnumb(res,"av_dur")] = `av_dur_`var''
	*/
}
			
clear
svmat res, names(col)

tostring dep sig sig_b note, replace

local r = 0
foreach var of global var {
local r = `r' + 1
replace dep = "`var'" if dep == "`r'"
}

rename b_f md

g time  = substr(dep,4,2)
destring time, replace

replace dep = substr(dep,1,3)

g upper = disc + 1.965 * abs(se)
g lower = disc - 1.965 * abs(se)

global haz "-0.02 -0.01 0 0.01 0.02 0.03"
global sur "-.03 -.02 -0.01 0 0.01 0.02 .03"
global dur "-2 -1 0 1 2"

global yhaz "Hazard Rate"
global ysur "Survival Rate"
global ydur "Censored Duration"

foreach dep in sur dur { // haz 
	twoway  (scatter disc time if dep == "`dep'" , mcolor(gs2) ) ///
	(rcap upper lower time if dep == "`dep'" , lcolor(gs2)  lp()), ///
	yline(0, lcolor(gs4)) ylabel( ${`dep'} , grid ) xlabel(3(3)36, grid glcolor(gs16)) legend(off) ///
	name(`dep',replace)  graphregion(fcolor(white) color(white)) ytitle(${y`dep'}, size(medsmall)) xtitle("Months since dismissal",size(medsmall)) ///
	title(,size(medsmall))
}


graph combine sur dur , title("2011-14 Payments") name(timing,replace) graphregion(fcolor(white) color(white)) col(1) xsize(10) ysize(14) imargin(0 0 0 0) // haz 



// ******************************  // FIGURE B8 - PART 2 // ****************************** //
use A_cash, clear

keep if year == 2014

set matsize 10000
mat res = J(10000,26,.)
mat coln res = cutoff av_dur dep disc se t sig disc_b se_b t_b sig_b bsel band band_b b_f pol obs control mon_i mon_f ten_min ten_max cause drop_x0 drop_mer note

global var "dur1 dur2 dur3 dur4 dur5 dur6 dur7 dur8 dur9 dur10 dur11 dur12 dur13 dur14 dur15 dur16 dur17 dur18 dur19 dur20 dur21 dur22 dur23 dur24 dur25 dur26 dur27 dur28 dur29 dur30 dur31 dur32 dur33 dur34 dur35 dur36 sur1 sur2 sur3 sur4 sur5 sur6 sur7 sur8 sur9 sur10 sur11 sur12 sur13 sur14 sur15 sur16 sur17 sur18 sur19 sur20 sur21 sur22 sur23 sur24 sur25 sur26 sur27 sur28 sur29 sur30 sur31 sur32 sur33 sur34 sur35 sur36" //

forval x = 1/36 { // everything monthly
cap g sur`x' = und > 30/7 * (`x')
cap g dur`x' = und
replace dur`x' = 30/7 * `x' if und >= 30/7 * `x'
}

* only eligible workers
keep if x<0
drop k kx x

g t = dtb - mdy(7,1,year(dtb))
g k = t>=0
g kt = k * t

******		RDD ON EACH VAR		******
local new = 0     // matrix line to write results
local var_n = 0   // var number
foreach var of global var {   
	local var_n = `var_n' + 1  

	qui su `var' if t > 0 & t <= ${bandrdbirth}
	local av_dur_`var' = r(mean)  // Average level to the right of the threshold

	// Regressions
	local new = `new' + 1
	qui reg `var' k kt t if abs(t)<= ${bandrdbirth} , vce(cluster cpf)
							
	mat res[`new',colnumb(res,"dep")] = `var_n'
	mat res[`new',colnumb(res,"disc")] = _b[k]
	mat res[`new',colnumb(res,"se")] =  _se[k]
	mat res[`new',colnumb(res,"t")] = abs(_b[k]/_se[k])  
	mat res[`new',colnumb(res,"pol")] = 1   
	mat res[`new',colnumb(res,"band")]  = ${bandrdbirth}
	mat res[`new',colnumb(res,"obs")] =	e(N)
	mat res[`new',colnumb(res,"control")] = 0			
	mat res[`new',colnumb(res,"av_dur")] = `av_dur_`var''
	*/
}
			
clear
svmat res, names(col)

tostring dep sig sig_b note, replace

local r = 0
foreach var of global var {
local r = `r' + 1
replace dep = "`var'" if dep == "`r'"
}

rename b_f md

g time  = substr(dep,4,2)
destring time, replace

replace dep = substr(dep,1,3)

g upper = disc + 1.965 * abs(se)
g lower = disc - 1.965 * abs(se)

global haz "-0.02 -0.01 0 0.01 0.02 0.03"
global sur "-.03 -.02 -0.01 0 0.01 0.02 .03"
global dur "-2 -1 0 1 2"

global yhaz "Hazard Rate"
global ysur "Survival Rate"
global ydur "Censored Duration"

foreach dep in sur dur { // haz 
	twoway  (scatter disc time if dep == "`dep'" , mcolor(gs2) ) ///
	(rcap upper lower time if dep == "`dep'" , lcolor(gs2)  lp()), ///
	yline(0, lcolor(gs4)) ylabel( ${`dep'} , grid ) xlabel(3(3)36, grid glcolor(gs16)) legend(off) ///
	name(`dep'2014,replace)  graphregion(fcolor(white) color(white)) ytitle(${y`dep'}, size(medsmall)) xtitle("Months since dismissal",size(medsmall)) ///
	title(,size(medsmall))
}


graph combine sur2014 dur2014 , title("2011-14 Payments") name(timing2014,replace) graphregion(fcolor(white) color(white)) col(1) xsize(10) ysize(14) imargin(0 0 0 0) // haz 

graph combine timing timing2014 , title() name(main,replace) graphregion(fcolor(white) color(white)) col(1) xsize(10) ysize(14) imargin(0 0 0 0) // haz 
graph export "${pgraph}/FigureB8.png", replace as(png) name(main) 

graph close




// ******************************  // FIGURE B9 // ****************************** //
use A_cash_othermonths, clear

* Month of Dismissal grouped
cap drop gd
g gd =  1 if dt_dism>=mdy(1,1,year) & dt_dism<mdy(4,1,year)
replace gd = 2 if dt_dism>=mdy(4,1,year) & dt_dism<mdy(6,1,year)
replace gd = 3 if dt_dism>=mdy(6,1,year) & dt_dism<mdy(7,16,year)
replace gd = 4 if dt_dism>=mdy(7,16,year) & dt_dism<mdy(9,1,year)
replace gd = 5 if dt_dism>=mdy(9,1,year) & dt_dism<=mdy(12,31,year)

// *** // effects on survival // *** //
set matsize 1000
mat res = J(1000,4,.)
mat coln res = samp dep disc se

* by group
global n = 0
forval gd = 1/2 {
di "workers dismissed group `gd'"
forval x = 4/41 { // n_remmedia pre_remmedia
global n = $n + 1
cap qui reg sura`x' k kx x if abs(x)<= ${band} & gd==`gd' [w=(${band}-abs(x))/${band}], vce(cluster cpf)
mat res[${n},colnumb(res,"samp")] = `gd'
mat res[${n},colnumb(res,"dep")] = `x'
mat res[${n},colnumb(res,"disc")] = _b[k]
mat res[${n},colnumb(res,"se")] = _se[k]
di "mon `x' est" round(_b[k],.001) " (" round(_se[k],.0001) ") tstat" _b[k]/_se[k]
}
}

forval gd = 4/4 {
di "workers dismissed group `gd'"
forval x = 6/41 { // n_remmedia pre_remmedia
global n = $n + 1
cap qui reg sura`x' k kx x if abs(x)<= ${band} & gd==`gd' [w=(${band}-abs(x))/${band}], vce(cluster cpf)
mat res[${n},colnumb(res,"samp")] = `gd'
mat res[${n},colnumb(res,"dep")] = `x'
mat res[${n},colnumb(res,"disc")] = _b[k]
mat res[${n},colnumb(res,"se")] = _se[k]
di "mon `x' est" round(_b[k],.001) " (" round(_se[k],.0001) ") tstat" _b[k]/_se[k]
}
}

forval gd = 5/5 {
di "workers dismissed group `gd'"
forval x = 8/41 { // n_remmedia pre_remmedia
global n = $n + 1
cap qui reg sura`x' k kx x if abs(x)<= ${band} & gd==`gd' [w=(${band}-abs(x))/${band}], vce(cluster cpf)
mat res[${n},colnumb(res,"samp")] = `gd'
mat res[${n},colnumb(res,"dep")] = `x'
mat res[${n},colnumb(res,"disc")] = _b[k]
mat res[${n},colnumb(res,"se")] = _se[k]
di "mon `x' est" round(_b[k],.001) " (" round(_se[k],.0001) ") tstat" _b[k]/_se[k]
}
}

clear
svmat res, names(col)

replace dep = dep-5
keep if dep>=0

g upper = disc + 1.965 * abs(se)
g lower = disc - 1.965 * abs(se)

global sur "-0.02 -0.01 0 0.01 0.02 0.03"

twoway (scatter disc dep if samp==1, mcolor(gs2) ) ///
(rcap upper lower dep if samp==1, lcolor(gs2)  lp()), ///
yline(0, lcolor(gs4)) ylabel( ${sur} , grid format(%12.2f) labs(vsmall) ) xlabel(0(3)36, grid glcolor(gs16)) legend(off) ///
name(sura,replace)  graphregion(fcolor(white) color(white)) ytitle(Survival Rate, size(medsmall)) xtitle("Calendar months relative to June in the dismissal year",size(medsmall)) ///
title("Workers Displaced between January and March",size(med))

twoway (scatter disc dep if samp==2, mcolor(gs2) ) ///
(rcap upper lower dep if samp==2, lcolor(gs2)  lp()), ///
yline(0, lcolor(gs4)) ylabel( ${sur} , grid format(%12.2f) labs(vsmall) ) xlabel(0(3)36, grid glcolor(gs16)) legend(off) ///
name(surb,replace)  graphregion(fcolor(white) color(white)) ytitle(Survival Rate, size(medsmall)) xtitle("Calendar months relative to June in the dismissal year",size(medsmall)) ///
title("Workers Displaced between April and May",size(med))

twoway (scatter disc dep if samp==4 & dep>=2, mcolor(gs2) ) ///
(rcap upper lower dep if samp==4 & dep>=2, lcolor(gs2)  lp()), ///
yline(0, lcolor(gs4)) ylabel( ${sur} , grid format(%12.2f) labs(vsmall) ) xlabel(0(3)36, grid glcolor(gs16)) legend(off) ///
name(surd,replace)  graphregion(fcolor(white) color(white)) ytitle(Survival Rate, size(medsmall)) xtitle("Calendar months relative to June in the dismissal year",size(medsmall)) ///
title("Workers Displaced between July 16th and August 31th",size(med))

twoway (scatter disc dep if samp==5, mcolor(gs2) ) ///
(rcap upper lower dep if samp==5, lcolor(gs2)  lp()), ///
yline(0, lcolor(gs4)) ylabel( ${sur} , grid format(%12.2f) labs(vsmall) ) xlabel(0(3)36, grid glcolor(gs16)) legend(off) ///
name(sure,replace)  graphregion(fcolor(white) color(white)) ytitle(Survival Rate, size(medsmall)) xtitle("Calendar months relative to June in the dismissal year",size(medsmall)) ///
title("Workers Displaced between September and December",size(med))

//surc 
graph combine sura surb surd sure, name(surv_eff,replace) graphregion(fcolor(white) color(white)) col(1) xsize(10) ysize(14) imargin(0 0 0 0) // haz 
graph export "${pgraph}/FigureB9.png", replace as(png) name(surv_eff)  

graph close



// ******************************  // FIGURE B10 B11 // ****************************** //
use B_ui, clear

global var "cash sur3 sur6 dur6 dur36 n_remmedr totinc totwage totmon remmedia school white ind_cons cash gend hor age_dism" //

// Labels
label var dur6 "Duration - 6 months" 
label var dur36 "Duration - 3 Years" 
label var sur3  "Survival - 3 months"
label var sur6 "Survival - 6 months"
label var n_remmedr "Monthly Earnings"
label var totinc "Total Income - 3 Years"
label var totwage "Av. Earnings - 3 Years"
label var totmon "Months Worked - 3 Years"

label var ind_cons "Construction Sector"
label var tempempr "Tenure at Lay-off (weeks)"
label var remmedia "Monthly Earnings m.w."
label var white "Prob(Worker is white)"
label var age_dism "Age at Dismissal"
label var cash "Av. Earnings (t-1) < 2 m.w."

// Select vars
keep ${var} cpf tempempr

g x = tempempr - 24
*replace x = . if tempempr >= 22  & tempempr <= 24 
g k = (x>=0)
g kx = k*x

global p1 "k kx x" // For Regressions - Splines

******************************************************
global bin_size = 0.25
global band_l = 16
global band_r = 30
global ci = 0          // ci = 1 for confidence intervals

gegen xb = cut(tempempr), at(-${band_l}(${bin_size})${band_r})
replace xb = xb + ${bin_size}/2
gegen tag = tag(xb)

foreach var of global var {

	cap drop av_`var'
	bysort xb: egen av_`var' = mean(`var')
	
	reg `var' ${p1} if tempempr >= 22 - ${bandui} & tempempr <= 24 + ${bandui} & (x<=-2|x>0), vce(cluster cpf)
	predict est_`var', xb

	su av_`var' if tag == 1 & tempempr >= ${band_l} & tempempr <= ${band_r}
	local r_min = round(r(min)*0.965,0.001)
	local r_max = round(r(max)*1.035,0.001)
	di `r_min'
	di `r_max'
	
	graph twoway (scatter av_`var' xb if tag == 1 & tempempr >= ${band_l} & tempempr <= ${band_r}, msize(small) msymbol(triangle) xline(22 24, lwidth(medthick)) ) ///
	(line est_`var' tempempr if tag == 1 & tempempr >= 22-${bandui} & tempempr < 24,  lcolor(black) lwidth(medthick) )  ///
	(line est_`var' tempempr if tag == 1 & tempempr >= 24 & tempempr <= 24+${bandui},  lcolor(black) lwidth(medthick) ) , ///
	ytitle("") xtitle(Tenure at Lay-off around 22/24 months) title(`: variable label `var'', size(med)) legend(off) name(`var',replace)  graphregion(fcolor(white) color(white)) ///
	yscale(r(`r_min' `r_max')) ylabel(`r_min' `r_max') xscale(r(16 30)) xlabel(16 18 20 22 24 26 28 30)

	cap drop est_`var'
	
	}
	
*** graph 1
	
	graph combine sur3 sur6 dur6 dur36 n_remmedr totinc totmon totwage, title() name(main, replace)  rows(4) xsize(10) ysize(15) graphregion(fcolor(white) color(white))
	graph export "${pgraph}/FigureB10.png", replace as(png) name(main)  width(1000) height(1500)
		
*** graph 3
	graph combine remmedia school white ind_cons cash gend hor age_dism, title() name(main, replace)  rows(4) xsize(10) ysize(15) graphregion(fcolor(white) color(white))
	graph export "${pgraph}/FigureB11.png", replace as(png) name(main)  width(1000) height(1500)

	
graph close
	

	
// ******************************  // FIGURE B12 // ****************************** //
use B_ui, clear

global var "dur1 dur2 dur3 dur4 dur5 dur6 dur7 dur8 dur9 dur10 dur11 dur12 dur13 dur14 dur15 dur16 dur17 dur18 dur19 dur20 dur21 dur22 dur23 dur24 dur25 dur26 dur27 dur28 dur29 dur30 dur31 dur32 dur33 dur34 dur35 dur36 sur1 sur2 sur3 sur4 sur5 sur6 sur7 sur8 sur9 sur10 sur11 sur12 sur13 sur14 sur15 sur16 sur17 sur18 sur19 sur20 sur21 sur22 sur23 sur24 sur25 sur26 sur27 sur28 sur29 sur30 sur31 sur32 sur33 sur34 sur35 sur36" //

// matrix for storing results
set matsize 1000
mat res = J(1000,20,.)
mat coln res = av_dur dep disc se t sig bsel band b_f pol obs control mon_i mon_f ten_min ten_max cause drop_x0 drop_mer note

// create vars
forval x = 1/36 { // everything monthly
cap drop sur`x'
g sur`x' = und > 30/7 * (`x')
cap drop dur`x'
g dur`x' = und
replace dur`x' = 30/7 * `x' if und >= 30/7 * `x'
}
	
// keeping only necessary variables
keep ${var} k kx x cpf tempempr mesdesli

******		RDD ON EACH VAR		******
local new = 0     // matrix line to write results
local var_n = 0   // var number
foreach var of global var {   
local var_n = `var_n' + 1

qui su `var' if x > -2.5 & x < 0
scalar av_dur = r(mean)

	local new = `new' + 1
	reg `var' k kx x if tempempr >= 22 - ${bandui} & tempempr <= 24 + ${bandui}, vce(cluster cpf)

	scalar b = _b[k]
	mat v_1 = e(V)
	scalar n_1 = e(N)
	
	mat res[`new',colnumb(res,"dep")] = `var_n'
	mat res[`new',colnumb(res,"disc")] = b
	mat res[`new',colnumb(res,"se")] = sqrt(v_1[1,1])	
	mat res[`new',colnumb(res,"t")] = abs(b/sqrt(v_1[1,1]))  
	mat res[`new',colnumb(res,"bsel")] = 1
	mat res[`new',colnumb(res,"pol")] = 1
	mat res[`new',colnumb(res,"band")]  = ${bandui}
	mat res[`new',colnumb(res,"b_f")]  = ${bandui}
	mat res[`new',colnumb(res,"obs")] =	n_1  		
	mat res[`new',colnumb(res,"av_dur")] = av_dur
	}
	
	
clear
svmat res, names(col)

tostring dep sig bsel note, replace

local r = 0
foreach var of global var {
local r = `r' + 1
replace dep = "`var'" if dep == "`r'"
}

rename b_f md

g time  = substr(dep,4,2)
destring time, replace

replace dep = substr(dep,1,3)

g upper = disc + 1.965 * abs(se)
g lower = disc - 1.965 * abs(se)

global haz "-0.02 0 0.02 0.04 0.06 0.08"
global sur "-0.02 0 0.02 0.04 0.06 0.08"
global dur "-1 0 1 2 3 4"

global yhaz "Hazard Rate"
global ysur "Survival Rate"
global ydur "Censored Duration"

foreach dep in sur dur { // haz 
twoway  (scatter disc time if dep == "`dep'", mcolor(gs2) ) ///
(rcap upper lower time if dep == "`dep'", lcolor(gs2)  lp()), ///
yline(0, lcolor(gs4)) ylabel( ${`dep'} , grid format(%12.2f)) xlabel(3(3)36, grid glcolor(gs16)) legend(off) ///
name(`dep',replace)  graphregion(fcolor(white) color(white)) ytitle(${y`dep'}, size(medsmall)) xtitle("Months since dismissal",size(small)) ///
title("${md`md'}",size(medsmall))
}



graph combine sur dur , name(surv_eff,replace) graphregion(fcolor(white) color(white)) col(1) // haz 
graph export "${pgraph}/FigureB12.png", replace as(png) name(surv_eff)  

graph close
	
	
	
// ******************************  // FIGURE B13 // ****************************** //
use B6_ten, clear

recode race (4=3) (6=4) (8=5)

g age = year - year(dtb)

* set tenure in september
replace dt_dism = mdy(12,31,year) if dt_dism==.
replace tempempr = tempempr - (dt_dism - mdy(10,1,year(dt_dism)))/30 if dt_dism>=mdy(10,1,year(dt_dism)) & dt_dism!=.

su tempempr, d
su remmedr, d

hist tempempr if tempempr <= 60, graphregion(fcolor(white) color(white)) bcolor(gs7) fcolor(gs14) name(ten_admin, replace) width(1) xtitle("Tenure months") ylabels(0 .03 .06 .09 .12)
hist remmedr if remmedr <= 2750, graphregion(fcolor(white) color(white)) bcolor(gs7) fcolor(gs14) name(wage_admin, replace) width(100) xtitle("Monthly earnings") ylabels(0 .0006 .0012)
hist race if race>=1 & race<=5, graphregion(fcolor(white) color(white)) bcolor(gs7) fcolor(gs14) width(.9999) name(race_admin, replace) xtitle("") xlabels(1.5 "Native" 2.5 "White" 3.5 "Black" 4.5 "Asian" 5.5 "Mixed") ylabels(0 .35 .7)
hist age if age>= 18 & age<=60, graphregion(fcolor(white) color(white)) bcolor(gs7) fcolor(gs14) name(age_admin, replace) width(.9999) xtitle("Age") ylabels(0 .02 .04)

clear
// *** // Tenure distribution in Survey - workers employed in september // *** //
use B5_survey_all, clear

keep if emp_form==1 // keep only formally employed for comparison
keep if v4105 <=3 // keep only households in urban  areas
drop if emp_agric == 1 // drop small share of agriculture workers (not in admin sample)

recode race (4=3) (6=4) (8=5)
g age = year - year(dtb)

su ten [w=v4729], d

hist ten if ten <= 60 [w=v4729], graphregion(fcolor(white) color(white)) bcolor(gs7) fcolor(gs14) name(ten_pnad, replace) width(1) xtitle("Tenure months") ylabels(0 .03 .06 .09 .12)
hist w if w <= 2750 [w=v4729], graphregion(fcolor(white) color(white)) bcolor(gs7) fcolor(gs14) name(wage_pnad, replace) width(100) xtitle("Monthly earnings") ylabels(0 .0006 .0012)
hist race if race>=1 & race<=5 [w=v4729], graphregion(fcolor(white) color(white)) bcolor(gs7) fcolor(gs14) width(.9999) name(race_pnad, replace) xtitle("") xlabels(1.5 "Native" 2.5 "White" 3.5 "Black" 4.5 "Asian" 5.5 "Mixed") ylabels(0 .35 .7)
hist age if age>= 18 & age<=60 [w=v4729], graphregion(fcolor(white) color(white)) bcolor(gs7) fcolor(gs14) name(age_pnad, replace) width(.9999) xtitle("Age") ylabels(0 .02 .04)

graph combine wage_admin race_admin age_admin ten_admin , title("Admin Data") name(out1, replace) rows(4) cols(1)  plotregion(margin(-1 0 -1 -1 )) imargin(vsmall) graphregion(fcolor(white) color(white)) 
graph combine wage_pnad race_pnad age_pnad ten_pnad , title("Survey Data") name(out2, replace) rows(4) cols(1)  plotregion(margin(-1 0 -1 -1 )) imargin(vsmall) graphregion(fcolor(white) color(white)) 

graph combine out1 out2 , title("") name(out, replace) rows(4) cols(2) xsize(7) ysize(10) plotregion(margin(-1 0 -1 -1 )) imargin(vsmall) graphregion(fcolor(white) color(white)) 
graph export "${pgraph}/FigureB13.png", name(out) replace width(2000)

graph close


// ******************************  // FIGURE B14 // ****************************** //
use B6_surveyrais_matched, clear // Matched Admin-survey sample

// Correlation tenure in the formal and informal data //
reg p_ten tempempr
global coef = int(_b[tempempr]*100)/100
global se = int(_se[tempempr]*100)/100

binscatter p_ten  tempempr , n(10) title(Tenure) ytitle("Tenure months - survey data") xtitle("Tenure months - admin data") ttext(27 18 "OLS coef.: ${coef} (${se})") name(tenure, replace)

su remsept if w_form!=.,d
reg w_form remsept if remsept<=2038
global coef = int(_b[remsept]*100)/100
global se = int(_se[remsept]*100)/100
binscatter w_form remsept if remsept<=2086, n(10) title(Earnings) ytitle("September earnings if formally employed - survey data") xtitle("monthly earnings if formally employed - admin data") ttext(1300 600 "OLS coef.: ${coef} (${se})") name(wage, replace)

graph combine tenure wage, title("") name(out, replace) rows(2) cols(1) xsize(7) ysize(10) graphregion(fcolor(white) color(white))  // plotregion(margin(-1 0 -1 -1 )) imargin(vsmall) 
graph export "${pgraph}/FigureB14.png", name(out) replace width(2000)

graph close

	

	
	
	
	
cap log close